package com.example.demo.Controller;


import com.example.demo.Entity.User;
import com.example.demo.Service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@RestController
@RequestMapping("/api/user")
@CrossOrigin(origins = "*")//允许所有同源访问，防止出现跨域问题
public class UserController {

    private final UserService userService;

    @Autowired
    public UserController(UserService userService) {
        this.userService = userService;
    }

    //注意前后端传输数据格式匹配问题
    //uniapp默认是前端以json格式传输，后端要接受需要这种格式(@RequestBody User user)


    //如果uniapp以表单格式传输,即在method ：POST 后加上
    // (          header: {
    //            'Content-Type': 'application/x-www-form-urlencoded'
    //          },)，则后端要接受需要这种格式(@ModelAttribute User user)或者是不加注解(User user)


    @PostMapping("/allUsers")
    public Map<String,Object> getAllUsers(){
       return userService.getAllUsersList();
    }

    @PostMapping("/searchLogByUserName")
    public Map<String,Object> searchLogByUserName(@RequestBody User user){//apipost不用加注解@RequestBody，uniapp要加该注解
        return userService.searchLogByUserName(user.getUsername());
    }

    @PostMapping("/ban")
    public Map<String,Object> banByUserName(@RequestBody User user){//apipost不用加注解@RequestBody，uniapp要加该注解
        return userService.banByUserName(user.getUsername());
    }

    @PostMapping("/active")
    public Map<String,Object> activeByUserName(@RequestBody User user){//apipost不用加注解@RequestBody，uniapp要加该注解
        return userService.activeByUserName(user.getUsername());
    }


    @PostMapping("/update")
    public Map<String, Object> updateUser(@RequestBody Map<String, String> request) {
        String username = request.get("username");
        String oldPassword = request.get("oldPassword");
        String newPassword = request.get("password");
        String newEmail = request.get("email");
        String newPhone = request.get("phone");

        Map<String, Object> response = new HashMap<>();
        Optional<User> userOptional = userService.findByUsername(username);

        if (userOptional.isPresent()) {
            User user = userOptional.get();
            if (!user.getPassword().equals(oldPassword)) {
                response.put("success", false);
                response.put("message", "旧密码不正确");
                return response;
            }

            //更新三个，密码，email，phone
            user.setPassword(newPassword);
            user.setEmail(newEmail);
            user.setPhoneNumber(newPhone);

            userService.updateUser(user.getId(),user);
            response.put("success", true);
            response.put("message", "更新成功");
        } else {
            response.put("success", false);
            response.put("message", "用户不存在");
        }
        return response;
    }

    @PostMapping("/getUserInfo")
    public Map<String, Object> getUserInfo(@RequestBody Map<String, String> request) {
        String username = request.get("username");

        Map<String, Object> response = new HashMap<>();
        Optional<User> userOptional = userService.findByUsername(username);

        if (userOptional.isPresent()) {
            response.put("success", true);
            response.put("user", userOptional.get());
        } else {
            response.put("success", false);
            response.put("message", "用户不存在");
        }
        return response;
    }

}

